<template>
	<div class="row">
		<div class="col-xs-12">
		<div id="role-scheduling-tab">
			<el-button :plain="true" type="danger"  class="againBut" @click="againBut">重新排课</el-button>
			<span class="lookError" @click="lookError">点击查看规则冲突列表</span>
			<div class="role-tab noBorderBottom">
				<ul>
					<li class="no-before room-tab">
						<i class="icon-step3-std-yes dh"></i><i class="icon-step3-std-no"></i><router-link  :to="{ path: '/automatic/Student' }" >学生课表</router-link>
					</li>
					<li class="active">
						<i class="icon-step3-teacher-yes"></i><i class="icon-step3-teacher-no"></i><router-link :to="{ path: '/automatic/Teacher' }" >教师课表</router-link>
					</li>
					<li  class="student-tab" v-if="showClassroom">
						<i class="icon-step3-class-yes"></i><i class="icon-step3-class-no"></i><router-link :to="{ path: '/automatic/Classroom' }">教室课表</router-link>
					</li>
					<li class="">
						<i class="icon-step3-all-yes"></i><i class="icon-step3-all-no"></i><router-link   :to="{ path: '/automatic/MasterTimetable' }" >总课表</router-link>
					</li>
				</ul>
			</div>
		</div>
		<el-select v-model="CourseValue" placeholder="请选择班级" class="selectClass" @change="changeCourse">
			<el-option v-for="item in QueryCourse"  :key="item.id" :label="item.name" :value="item.name">
			</el-option>
		</el-select>
		<el-select v-model="TeacherId" placeholder="请选择教师" class="selectClass" @change="changeTeacher">
			<el-option v-for="item in TeacherList"  :key="item.id" :label="item.teacherName" :value="item.id">
			</el-option>
		</el-select>

		<div class="col-xs-24 selectClass overflow">
			<div class="describe_title">
				调课说明：
			</div>
			<div class="describe_colorBox green"></div>
			<div class="describe_text">
				可调空闲区域
			</div>
			<div class="describe_colorBox yellow"></div>
			<div class="describe_text">
				与预设规则冲突
			</div>
			<div class="describe_colorBox orange"></div>
			<div class="describe_text">
				该区域已经设置不排课
			</div>
		</div>

		<div class="col-xs-24 selectClass overflow">
			<div class="describe_title2">
				温馨提示：
			</div>
			<div class="describe_textBox">
				<div class="describe_item">
					<div class="describe_colorBox2 green"></div>
					<div class="describe_colorBox2 yellow"></div>
					<div class="describe_colorBox2 orange"></div>
					<div class="describe_text2">
						三种颜色下都可以调整课表，白色区域不可以调整课表区域。
					</div>
				</div>
				<div class="describe_item">
					<div class="describe_colorBox2 yellow"></div>
					<div class="describe_text2">
						与预设的合班规则、不连堂规则、教室平齐规则、周任课/日任课规则、连上规则有冲突
					</div>
				</div>
				<div class="describe_item noMB10">
					<div class="describe_colorBox2 orange"></div>
					<div class="describe_text2">
						与预设的不排课规则冲突、包括年级不排课、班级不排课、课程不排课、老师不排课
					</div>
				</div>
			</div>
		</div>

		<div class="block textAndBtn">
			<span class="demonstration">教师课程表</span>
			<span class="wrapper export" @click="exportTable">
				    <el-button type="primary" icon="upload2" >导出所有教师课程表</el-button>
				  </span>
			<div class="clear"></div>
		</div>
		<template v-if="classType == 1">
			<el-table :data="getCourseList" v-loading="loading" :element-loading-text="loadingText" @cell-click="getCellClick" border style="width: 100%">
				<el-table-column prop="num" label="节次" align="center">
					<template scope="scope">
							<div class="cell_white">
									{{ scope.row.num}}
							</div>
					</template>
				</el-table-column>
				<el-table-column prop="Monday" label="星期一" align="center">
					<template scope="scope">
							<div :color="scope.row.Monday.color" :class="scope.row.Monday.color" :posX="scope.row.Monday.posX" :posY="scope.row.Monday.posY">
									{{ scope.row.Monday.name}}
							</div>
					</template>
				</el-table-column>
				<el-table-column prop="Tuesday" label="星期二" align="center">
					<template scope="scope">
							<div :color="scope.row.Tuesday.color" :class="scope.row.Tuesday.color" :posX="scope.row.Tuesday.posX" :posY="scope.row.Tuesday.posY">
									{{ scope.row.Tuesday.name}}
							</div>
					</template>
				</el-table-column>
				<el-table-column prop="Wednesday" label="星期三" align="center">
					<template scope="scope">
							<div :color="scope.row.Wednesday.color" :class="scope.row.Wednesday.color" :posX="scope.row.Wednesday.posX" :posY="scope.row.Wednesday.posY">
									{{ scope.row.Wednesday.name}}
							</div>
					</template>
				</el-table-column>
				<el-table-column prop="Thursday" label="星期四" align="center">
					<template scope="scope">
							<div :color="scope.row.Thursday.color" :class="scope.row.Thursday.color" :posX="scope.row.Thursday.posX" :posY="scope.row.Thursday.posY">
									{{ scope.row.Thursday.name}}
							</div>
					</template>
				</el-table-column>
				<el-table-column prop="Friday" label="星期五" align="center">
					<template scope="scope">
							<div :color="scope.row.Friday.color" :class="scope.row.Friday.color" :posX="scope.row.Friday.posX" :posY="scope.row.Friday.posY">
									{{ scope.row.Friday.name}}
							</div>
					</template>
				</el-table-column>
			</el-table>
		</template>

		<template v-if="classType == 2">
			<el-row>
			  <el-col :span="14">
					<el-table :data="getCourseList" v-loading="loading" :element-loading-text="loadingText" @cell-click="getCellClick2" border style="width: 100%">
						<el-table-column prop="num" label="节次" align="center">
							<template scope="scope">
									<div class="cell_white">
											{{ scope.row.num}}
									</div>
							</template>
						</el-table-column>
						<el-table-column prop="Monday" label="星期一" align="center">
							<template scope="scope">
									<div :color="scope.row.Monday.color" :class="scope.row.Monday.color" :posX="scope.row.Monday.posX" :posY="scope.row.Monday.posY">
											{{ scope.row.Monday.name}}
									</div>
							</template>
						</el-table-column>
						<el-table-column prop="Tuesday" label="星期二" align="center">
							<template scope="scope">
									<div :color="scope.row.Tuesday.color" :class="scope.row.Tuesday.color" :posX="scope.row.Tuesday.posX" :posY="scope.row.Tuesday.posY">
											{{ scope.row.Tuesday.name}}
									</div>
							</template>
						</el-table-column>
						<el-table-column prop="Wednesday" label="星期三" align="center">
							<template scope="scope">
									<div :color="scope.row.Wednesday.color" :class="scope.row.Wednesday.color" :posX="scope.row.Wednesday.posX" :posY="scope.row.Wednesday.posY">
											{{ scope.row.Wednesday.name}}
									</div>
							</template>
						</el-table-column>
						<el-table-column prop="Thursday" label="星期四" align="center">
							<template scope="scope">
									<div :color="scope.row.Thursday.color" :class="scope.row.Thursday.color" :posX="scope.row.Thursday.posX" :posY="scope.row.Thursday.posY">
											{{ scope.row.Thursday.name}}
									</div>
							</template>
						</el-table-column>
						<el-table-column prop="Friday" label="星期五" align="center">
							<template scope="scope">
									<div :color="scope.row.Friday.color" :class="scope.row.Friday.color" :posX="scope.row.Friday.posX" :posY="scope.row.Friday.posY">
											{{ scope.row.Friday.name}}
									</div>
							</template>
						</el-table-column>
					</el-table>
			  </el-col>
			  <el-col :span="1">&nbsp;</el-col>
			  <el-col :span="9">
					<el-table :data="getClassroomList" v-loading="loading2" :element-loading-text="loading2Text" @cell-click="getCellClick3" border style="width: 100%">
						<el-table-column prop="ClassroomList" label="教室列表" align="center">
							<template scope="scope">
									<div :class="scope.row.ClassroomList.color" :roomId="scope.row.ClassroomList.roomId" :index="scope.row.ClassroomList.index">
											{{ scope.row.ClassroomList.name}}
									</div>
							</template>
						</el-table-column>
						<el-table-column prop="CurriculumList" label="课程信息" align="center">
							<template scope="scope">
									<div :color="scope.row.CurriculumList.color" :class="scope.row.CurriculumList.color" :roomId="scope.row.CurriculumList.roomId" :index="scope.row.ClassroomList.index">
											{{ scope.row.CurriculumList.name}}
									</div>
							</template>
						</el-table-column>
					</el-table>
			  </el-col>
			</el-row>
		</template>
		<el-dialog title="规则冲突列表" :visible.sync="dialogTableVisible" class="dialogError" v-loading="loadingError" element-loading-text="拼命加载中" style="width: 100%;">
			<el-col :span="24">
					<div class="grid-content bg-purple">
						<div v-for="o in errorList" :key="o" class="text item">{{o}}</div>
					</div>
			</el-col>
		</el-dialog>
	</div>
</div>
</template>

<script>
	import utils from '@/assets/js/urlConfig.js'
	export default {
		data() {
			return {
				CourseValue: "",
				CourseType: "",
				TeacherValue:"",
				QueryCourse:[],
				TeacherList:[],
				TeacherId:"",
				type:"teacher",
				taskId:"",
				tnId:"",
				exportUrl:"",
				classType:"",
				classType2:"",
				sectionNum:0,
				loading:true,
				loadingText:"拼命加载中",
				loading2:false,
				loading2Text:"拼命加载中",
				getCourseList: [],
				getCourseList2: [],
				source:"",
				target:"",
				getClassroomList:[],
				getClassroomList2:[],
				ClassroomList1:[],
				ClassroomList2:[],
				ClassroomList3:[],
				showClassroom:false,
				dialogTableVisible:false,
				errorList:[],
				loadingError:false,
			}
		},
		mounted() {
			this.getSessionData();
			this.GetQueryCourseData();
			// this.getClassroomRight();
		},
		methods: {
			// 获取sess
			getSessionData(){
				this.taskId = sessionStorage.getItem('taskId');
				this.tnId = sessionStorage.getItem('tnId');
				this.classType = sessionStorage.getItem('classType');
				this.classType2 = sessionStorage.getItem('classType');
				if(sessionStorage.getItem('classType')!= 1 || sessionStorage.getItem('gradeName')!="高一年级"){
						this.showClassroom = true
				}
			},
			// 获取课程列表
			GetQueryCourseData() {
			 this.$http.get(utils.UrlConfig.GetQueryCourse+"&taskId="+this.taskId)
			   .then(res => {
			     res = res.data
			     if (res.rtnCode === "0000000") {
						 // if(this.classType == "1"){
							//  this.QueryCourse = res.bizData;
						 // }else{
							//  let list = [];
							//  for(let i=0;i<res.bizData.length;i++){
							// 	 if(res.bizData[i].name != "语文" && res.bizData[i].name != "数学" && res.bizData[i].name != "英语"){
							// 		 list.push(res.bizData[i]);
							// 	 }
							//  }
							//  this.QueryCourse = list;
						 // }
						 this.QueryCourse = res.bizData;
			       		 this.CourseValue = this.QueryCourse[0].name;
						 this.CourseType = this.QueryCourse[0].courseType;
						 if(this.classType == "2"){
							 if(this.CourseType == "5"){
								 this.classType = "2";
							 }else{
								 this.classType = "1";
							 }
						 }
			     } else {
			         this.$message.error(res.msg);
			     }
			 }).catch(error => {
			     console.log(error)
			 })
			},
			// 获取教师列表
			getTeacherData(taskId,CourseValue){
			 this.loading = true;
			 this.$http.get(utils.UrlConfig.getTeacherList2+"&taskId="+taskId+"&tnId="+this.tnId+"&teacherCourse="+CourseValue)
			   .then(res => {
			     res = res.data

			     if (res.rtnCode === "0000000") {
			        this.TeacherList = res.bizData;

			        // 展示用
				     var ZLP = this.TeacherList[0].id;
				     for(var i = 0 ; i < this.TeacherList.length ; i++){
				     	if(this.TeacherList[i].teacherName.indexOf('张莉萍') != -1){
				     		ZLP = this.TeacherList[i].id;
				     		break;
				     	}
				     }
			        this.TeacherId = ZLP;
			        // this.TeacherId = this.TeacherList[0].id;
			     } else {
			         this.$message.error(res.msg);
							 this.loading = false;
			     }
			 }).catch(error => {
			     console.log(error)
					 this.loading = false;
			 })
			},
			// 根据老师获取课表
			getResult(type,taskId,tnId,TeacherId){
			 let getCourseList = [];
 			 this.loading = true;
 			 this.$http.get(utils.UrlConfig.getResultByTeacher+"&taskId="+taskId+"&tnId="+tnId+"&param={teacherId:"+this.TeacherId+"}")
 			   .then(res => {
 			     res = res.data
 			     if (res.rtnCode === "0000000") {
 						 this.nameList = res.bizData.result.week;
						 this.sectionNum = res.bizData.result.week[0].length;
 							for(var i = 0 ; i < res.bizData.result.week[0].length; i++){
 								var json = {};
 								json.num = "第"+(i+1)+"节";
 								for(var k = 0 ; k < res.bizData.result.week.length ; k++){
 									if(k == 0){
 										json.Monday = {
 											"name": res.bizData.result.week[k][i],
 											"color":"cell_white",
 											"posX": k,
 											"posY": i,
 										};
 									}else if(k == 1){
 										json.Tuesday = {
 											"name": res.bizData.result.week[k][i],
 											"color":"cell_white",
 											"posX": k,
 											"posY": i,
 										};
 									}else if(k == 2){
 										json.Wednesday = {
 											"name": res.bizData.result.week[k][i],
 											"color":"cell_white",
 											"posX": k,
 											"posY": i,
 										};
 									}else if(k == 3){
 										json.Thursday = {
 											"name": res.bizData.result.week[k][i],
 											"color":"cell_white",
 											"posX": k,
 											"posY": i,
 										};
 									}else if(k == 4){
 										json.Friday = {
 											"name": res.bizData.result.week[k][i],
 											"color":"cell_white",
 											"posX": k,
 											"posY": i,
 										};
 									}
 								}
 								getCourseList.push(json);
 							}
 							this.getCourseList = getCourseList;
 							this.loading = false;
 							// console.log(this.getCourseList);
 			     } else {
 			         this.$message.error(res.msg);
							 this.loading = false;
 			     }
 			 }).catch(error => {
 			     console.log(error)
					 this.loading = false;
 			 })
			},
			// 导出
			exportTable(){
				window.location.href = utils.UrlConfig.exportTeacher+"&taskId="+this.taskId+"&tnId="+this.tnId;
			},
			// 课程下拉框change事件
			changeCourse(value){
				for (var i = 0; i < this.QueryCourse.length; i++) {
					if(this.QueryCourse[i].name == value){
						this.CourseType = this.QueryCourse[i].courseType;
						if(this.CourseType == "5"){
							this.classType = "2";
						}else{
							this.classType = "1";
						}
					}
				}
				this.getTeacherData(this.taskId,this.CourseValue);
				this.source = "";
			},
			// 老师下拉框change事件
			changeTeacher(value){
				this.getResult(this.type,this.taskId,this.tnId,this.TeacherId);
				this.source = "";
			},
			getCellClick(row, column, cell, event){
				// alert(row[column.property].posX+"==="+row[column.property].posY);
				if(this.source == ""){
					if(row[column.property].name != ""){
						this.source = "["+row[column.property].posX+","+row[column.property].posY+"]";
					}
				}else{
					this.target = "["+row[column.property].posX+","+row[column.property].posY+"]";
					if(this.source == this.target){
						this.getResult(this.type,this.taskId,this.tnId,this.ClassroomId);
						this.source = "";
						this.target = "";
						return;
					}else{
						let color = row[column.property].color;
						if(color == "cell_white"){
							this.$message.error("白色不能调课！");
							// this.getResult(this.type,this.taskId,this.tnId,this.ClassroomId);
							// this.source = "";
							// this.target = "";
						}else{
							this.exchange(this.TeacherId,this.taskId,this.tnId,this.source,this.target);
						}
						return;
					}
				}
				if(row[column.property].name != ""){
					// console.log(this.CourseType+"=="+this.classType+"=="+this.classType2);
					if(this.classType == "1" && this.classType2 == "1"){
							this.queryStatusByClassCoord(this.ClassroomId,this.taskId,this.tnId,row[column.property].posX,row[column.property].posY);
					}else{
						  this.queryEduStatusByClassCoord2(this.ClassroomId,this.taskId,this.tnId,row[column.property].posX,row[column.property].posY);
					}
				}
			},
			getCellClick2(row, column, cell, event){
				let posX = row[column.property].posX;
				let posY = row[column.property].posY;
				if(row[column.property].name != ""){
					if(this.source == ""){
						this.getCellTextRed(parseInt(posX),parseInt(posY));
						this.source = "["+row[column.property].posX+","+row[column.property].posY+"]";
						this.getClassroomRight();
					}else{
						this.target = "["+row[column.property].posX+","+row[column.property].posY+"]";
						if(this.source == this.target){
							this.getResult(this.type,this.taskId,this.tnId,this.TeacherId);
							this.source = "";
							this.target = "";
							this.getClassroomList = [];
							this.loading2 = false;
						}else{
							this.queryCurrTimeCourse(this.target);
						}
					}
				}else{
					if(this.source != ""){
						this.target = "["+row[column.property].posX+","+row[column.property].posY+"]";
						this.queryCurrTimeCourse(this.target);
					}
				}

			},
			getCellClick3(row, column, cell, event){
				let roomid = row[column.property].roomId;
				let color = row[column.property].color;
				if(color == "cell_white"){
					this.$message.error("白色不能调课！");
					return;
				}
				if(roomid){
					 this.loading = true;
					 this.loading2Text = "正在调课中...";
					 this.loading2 = true;
		 			 this.$http.post(utils.UrlConfig.exchangeByTeacherTeach+"&taskId="+this.taskId+"&id="+this.TeacherId+"&tnId="+this.tnId+"&source="+this.source+"&target="+this.target+"&targetRoomId="+roomid)
		 			   .then(res => {
		 			     res = res.data
		 			     if (res.rtnCode == '0000000' && res.bizData == true) {
								 this.$message({
									 message: '调课成功！',
									 type: 'success'
								 });
								 this.source = "";
								 this.getResult(this.type,this.taskId,this.tnId,this.ClassroomId);
								//  this.getClassroomRight();
								this.getClassroomList = [];
								this.loading2 = false;
		 			     } else {
		 			         this.$message.error(res.msg);
		 							 this.loading = false;
									 this.loading2 = false;
		 			     }
		 			 }).catch(error => {
		 			     console.log(error)
		 					 this.loading = false;
							 this.loading2 = false;
		 			 })
				}
			},
			//行政班调课
			queryStatusByClassCoord(Id,taskId,tnId,posX,posY){
			 this.loadingText = "正在积极准备可调区域，请耐心等待！";
			 this.loading = true;
			 this.$http.post(utils.UrlConfig.queryStatusByTeacherCoord+"&taskId="+taskId+"&id="+this.TeacherId+"&tnId="+tnId+"&coord=["+posX+","+posY+"]")
			   .then(res => {
			     res = res.data
			     if (res.rtnCode == '0000000' && res.bizData == true) {
							 this.queryClassByCoord(posX,posY);
			     } else {
						 	 this.source = [];
			         this.$message.error(res.msg);
							 this.loading = false;
			     }
			 }).catch(error => {
			     console.log(error)
					 this.loading = false;
					 this.source = [];
			 })
			},
			//教学班调课
			queryEduStatusByClassCoord(Id,taskId,tnId,posX,posY){
			 this.loading = true;
			 this.$http.post(utils.UrlConfig.queryEduStatusByClassCoord+"&taskId="+taskId+"&id="+this.TeacherId+"&tnId="+tnId+"&coord=["+posX+","+posY+"]")
			   .then(res => {
			     res = res.data
			     if (res.rtnCode == '0000000' && res.bizData == true) {
						   this.colorScheduleResult();
			     } else {
			         this.$message.error(res.msg);
							 this.loading = false;
							 this.source = [];
			     }
			 }).catch(error => {
			     console.log(error)
					 this.loading = false;
					 this.source = [];
			 })
			},
			//教学班调课
			queryEduStatusByClassCoord2(Id,taskId,tnId,posX,posY){
				 this.loadingText = "正在积极准备可调区域，请耐心等待！";
				 this.loading = true;
				 this.$http.post(utils.UrlConfig.queryEduStatusByTeacherCoord+"&taskId="+taskId+"&id="+this.TeacherId+"&tnId="+tnId+"&coord=["+posX+","+posY+"]"+"&target=[0,0]")
				   .then(res => {
				     res = res.data
				     if (res.rtnCode == '0000000' && res.bizData == true) {
								 this.queryClassByCoord(posX,posY);
				     } else {
							 	 this.source = [];
				         this.$message.error(res.msg);
								 this.loading = false;
				     }
				 }).catch(error => {
				     console.log(error)
						 this.loading = false;
						 this.source = [];
				 })
			},
			queryClassByCoord(posX,posY){
				 let getCourseList = [];
				 this.loading = true;
	 			 this.$http.post(utils.UrlConfig.queryClassByCoord+"&taskId="+this.taskId+"&id="+this.TeacherId+"&tnId="+this.tnId+"&coord=["+posX+","+posY+"]")
	 			   .then(res => {
	 			     res = res.data
	 			     if (res.rtnCode == '0000000') {
							 	 this.nameList2 = res.bizData;
								 this.colorScheduleResult();
	 			     } else {
	 			         this.$message.error(res.msg);
	 							 this.loading = false;
	 							 this.source = [];
	 			     }
	 			 }).catch(error => {
	 			     console.log(error)
	 					 this.loading = false;
	 					 this.source = [];
	 			 })
			},
			colorScheduleResult(){
				this.loading = true;
				this.$http.get(utils.UrlConfig.result+"&taskId="+this.taskId+"&tnId="+this.tnId)
 			   .then(res => {
 			     res = res.data
 			     if (res.rtnCode == '0000000') {
						 var state = res.bizData;
						 switch (state) {
                    case "0":
                        // console.log("调课中");
                        that.items2 = setInterval(function () {
                            this.colorScheduleResult();
                        }, 30000);
                        break;
                    case "1":
                        // console.log("调课成功");
                        this.scheduleTaskSuccess();
                        break;
                    case "-1":
                        // console.log("调课失败(数据异常)");
												// this.getDesc();
												this.$message.error("调课失败！");
												this.loading = false;
												this.source = [];
                        break;
                    case "-2":
                        // console.log("调课失败(系统异常)");
												// this.getDesc();
												this.$message.error("调课失败！");
												this.loading = false;
												this.source = [];
                        break;
                    default:
                        break;

                }
 			     } else {
 			         this.$message.error(res.msg);
							 this.loading = false;
							 this.source = [];
 			     }
 			 }).catch(error => {
 			     console.log(error)
					 this.loading = false;
					 this.source = [];
 			 })
			},
			scheduleTaskSuccess(){
				this.loading = true;
				this.$http.get(utils.UrlConfig.success+"&taskId="+this.taskId+"&tnId="+this.tnId)
				 .then(res => {
					 res = res.data
					 if (res.rtnCode == '0000000') {
						 let tableList = [];
						 let itmeList = [];
						 for(let i=0;i<res.bizData.length;i++){
							 let itme = res.bizData[i];
							 itmeList.push(itme);
							 if((i+1)%this.sectionNum==0){
								 tableList.push(itmeList);
								 itmeList = [];
							 }
						 }
						 this.colorList = tableList;
						 this.getCourseList2 = [];

						 for(var i = 0 ; i < this.nameList[0].length; i++){
							 var json = {};
							 json.num = "第"+(i+1)+"节";
							 for(var k = 0 ; k < this.nameList.length ; k++){
								 var colorValue = '';
                    switch (this.colorList[k][i]) {
                        case "0":
                            colorValue = "cell_white";
                            //0 白色
                            break;
                        case "1":
                            colorValue = "cell_orange";
                            //1红色
                            break;
                        case "2":
                            colorValue = "cell_yellow";
                            //2黄色
                            break;
                        case "3":
                            colorValue = "cell_green";
                            //3绿色
                            break;
                        default:
                            break;
                    }
								 if(k == 0){
									 json.Monday = {
										 "name": this.nameList[k][i] == ""?this.nameList2[k][i]:this.nameList[k][i],
										 "color":colorValue,
										 "posX": k,
										 "posY": i,
									 };
								 }else if(k == 1){
									 json.Tuesday = {
										 "name": this.nameList[k][i] == ""?this.nameList2[k][i]:this.nameList[k][i],
										 "color":colorValue,
										 "posX": k,
										 "posY": i,
									 };
								 }else if(k == 2){
									 json.Wednesday = {
										 "name": this.nameList[k][i] == ""?this.nameList2[k][i]:this.nameList[k][i],
										 "color":colorValue,
										 "posX": k,
										 "posY": i,
									 };
								 }else if(k == 3){
									 json.Thursday = {
										 "name": this.nameList[k][i] == ""?this.nameList2[k][i]:this.nameList[k][i],
										 "color":colorValue,
										 "posX": k,
										 "posY": i,
									 };
								 }else if(k == 4){
									 json.Friday = {
										 "name": this.nameList[k][i] == ""?this.nameList2[k][i]:this.nameList[k][i],
										 "color":colorValue,
										 "posX": k,
										 "posY": i,
									 };
								 }
							 }
							 this.getCourseList2.push(json);
						 }
						 this.getCourseList = this.getCourseList2;
						 this.loading = false;
					 } else {
							 this.$message.error(res.msg);
							 this.loading = false;
							 this.source = [];
					 }
			 }).catch(error => {
					 console.log(error)
					 this.loading = false;
					 this.source = [];
			 })
		 },
		 exchange(Id,taskId,tnId,source,target){
			 this.loadingText = "正在调课中...";
			 this.loading = true;
			 this.$http.post(utils.UrlConfig.exchangeByTeacher+"&taskId="+taskId+"&id="+Id+"&tnId="+tnId+"&source="+source+"&target="+target)
			   .then(res => {
			     res = res.data
			     if (res.rtnCode === "0000000") {
						 	 if(res.bizData){
								 this.$message({
				          message: '调课成功！',
				          type: 'success'
				        });
								this.source = [];
								this.getResult(this.type,this.taskId,this.tnId,this.ClassroomId);
							}else{
								this.$message.error("调课失败！");
								this.getResult(this.type,this.taskId,this.tnId,this.ClassroomId);
							}
			     } else {
			         this.$message.error(res.msg);
							 this.loading = false;
			     }
			 }).catch(error => {
			     console.log(error)
					 this.loading = false;
			 })
		 },
		 getCellTextRed(posX,posY){
			 let getCourseList = [];
			 this.loading = true;
				this.$http.get(utils.UrlConfig.getResultByTeacher+"&taskId="+this.taskId+"&tnId="+this.tnId+"&param={teacherId:"+this.TeacherId+"}")
					.then(res => {
						res = res.data
						if (res.rtnCode === "0000000") {
							 for(var i = 0 ; i < res.bizData.result.week[0].length; i++){
								 var json = {};
								 json.num = "第"+(i+1)+"节";
								 for(var k = 0 ; k < res.bizData.result.week.length ; k++){
									 if(k == 0){
										 if(k == posX && i == posY){
											 json.Monday = {
												 "name": res.bizData.result.week[k][i],
												 "color":"cell_red",
												 "posX": k,
												 "posY": i,
											 };
										 }else{
											 json.Monday = {
												 "name": res.bizData.result.week[k][i],
												 "color":"cell_white",
												 "posX": k,
												 "posY": i,
											 };
										 }
									 }else if(k == 1){
										 if(k == posX && i == posY){
											 json.Tuesday = {
												 "name": res.bizData.result.week[k][i],
												 "color":"cell_red",
												 "posX": k,
												 "posY": i,
											 };
										 }else{
											 json.Tuesday = {
												 "name": res.bizData.result.week[k][i],
												 "color":"cell_white",
												 "posX": k,
												 "posY": i,
											 };
										 }
									 }else if(k == 2){
										 if(k == posX && i == posY){
											 json.Wednesday = {
												 "name": res.bizData.result.week[k][i],
												 "color":"cell_red",
												 "posX": k,
												 "posY": i,
											 };
										 }else{
											 json.Wednesday = {
												 "name": res.bizData.result.week[k][i],
												 "color":"cell_white",
												 "posX": k,
												 "posY": i,
											 };
										 }
									 }else if(k == 3){
										 if(k == posX && i == posY){
											 json.Thursday = {
												 "name": res.bizData.result.week[k][i],
												 "color":"cell_red",
												 "posX": k,
												 "posY": i,
											 };
										 }else{
											 json.Thursday = {
												 "name": res.bizData.result.week[k][i],
												 "color":"cell_white",
												 "posX": k,
												 "posY": i,
											 };
										 }
									 }else if(k == 4){
										 if(k == posX && i == posY){
											 json.Friday = {
												 "name": res.bizData.result.week[k][i],
												 "color":"cell_red",
												 "posX": k,
												 "posY": i,
											 };
										 }else{
											 json.Friday = {
												 "name": res.bizData.result.week[k][i],
												 "color":"cell_white",
												 "posX": k,
												 "posY": i,
											 };
										 }
									 }
								 }
								 getCourseList.push(json);
							 }
							 this.getCourseList = getCourseList;
							 this.loading = false;
							 // console.log(this.getCourseList);
						} else {
								this.$message.error(res.msg);
							this.loading = false;
						}
				}).catch(error => {
						console.log(error)
					this.loading = false;
				})
		 },
		 getClassroomRight(){
			 this.loading2Text = "拼命加载中";
			 this.loading2 = true;
			 this.getClassroomList = [];
			 this.$http.get(utils.UrlConfig.queryRoom+"&taskId="+this.taskId+"&tnId="+this.tnId)
			   .then(res => {
			     res = res.data
			     if (res.rtnCode === "0000000") {
						 this.ClassroomList1 = res.bizData;
						 for(var k = 0 ; k < res.bizData.length ; k++){
							 var json = {};
							 json.ClassroomList = {
								 "name": res.bizData[k].roomName,
								 "color":"cell_white",
								 "roomId": res.bizData[k].roomId,
								 "index": k,
							 };
							 json.CurriculumList = {
								 "name": "",
								 "color":"cell_white",
								 "roomId": "",
								 "index": k,
							 };
							 this.getClassroomList.push(json);
						 }
						//  console.log(this.getClassroomList);
						 this.loading2 = false;
			     } else {
			         this.$message.error(res.msg);
							 this.loading2 = false;
			     }
			 }).catch(error => {
			     console.log(error)
					 this.loading2 = false;
			 })
		 },
		 queryCurrTimeCourse(source){
			 this.loading2Text = "正在积极准备可调区域，请耐心等待！";
			 this.loading2 = true;
			 this.$http.get(utils.UrlConfig.queryCurrTimeCourse+"&taskId="+this.taskId+"&tnId="+this.tnId+"&id="+this.TeacherId+"&source="+source)
			   .then(res => {
			     res = res.data
			     if (res.rtnCode === "0000000") {
						 this.ClassroomList2 = res.bizData;
						 let getClassroomLists = [];
						 this.getClassroomList = [];
						 for(var k = 0 ; k < this.ClassroomList1.length ; k++){
							 var json = {};
							 json.ClassroomList = {
								 "name": this.ClassroomList1[k].roomName,
								 "color":"cell_white",
								 "roomId": this.ClassroomList1[k].roomId,
								 "index": k,
							 };
							 if(k < res.bizData.length){
								 json.CurriculumList = {
									 "name": res.bizData[k].content,
									 "color":"cell_white",
									 "roomId": res.bizData[k].roomId,
									 "index": k,
								 };
							 }else{
								 json.CurriculumList = {
									 "name": "",
									 "color":"cell_white",
									 "roomId": "",
									 "index": k,
								 };
							 }
							 getClassroomLists.push(json);
						 }
						 this.getClassroomList = getClassroomLists;
						 this.queryEduStatusByTeacherCoord();
						//  this.loading2 = false;
			     } else {
			         this.$message.error(res.msg);
							 this.loading2 = false;
			     }
			 }).catch(error => {
			     console.log(error)
					 this.loading2 = false;
			 })
		 },
		 queryEduStatusByTeacherCoord(){
			 this.loading2 = true;
			 this.$http.get(utils.UrlConfig.queryEduStatusByTeacherCoord+"&taskId="+this.taskId+"&tnId="+this.tnId+"&id="+this.TeacherId+"&coord="+this.source+"&target="+this.target)
				.then(res => {
					res = res.data
					if (res.rtnCode == '0000000' && res.bizData == true) {
						this.TeacherColorScheduleResult();
					} else {
							this.$message.error(res.msg);
							this.loading2 = false;
					}
			}).catch(error => {
					console.log(error)
					this.loading2 = false;
			})
		 },
		 TeacherColorScheduleResult(){
			 this.loading2 = true;
			 this.$http.get(utils.UrlConfig.result+"&taskId="+this.taskId+"&tnId="+this.tnId)
				.then(res => {
					res = res.data
					if (res.rtnCode == '0000000') {
						var state = res.bizData;
						switch (state) {
									 case "0":
											//  console.log("调课中");
											 setInterval(function () {
													 this.TeachercolorScheduleResult();
											 }, 30000);
											 break;
									 case "1":
											//  console.log("调课成功");
											 this.TeacherSuccess();
											 break;
									 case "-1":
											//  console.log("调课失败(数据异常)");
											//  this.getDesc();
											 this.$message.error("调课失败！");
											 this.loading2 = false;
											 break;
									 case "-2":
											//  console.log("调课失败(系统异常)");
											//  this.getDesc();
											 this.$message.error("调课失败！");
											 this.loading2 = false;
											 break;
									 default:
											 break;

							 }
					} else {
							this.$message.error(res.msg);
							this.loading2 = false;
					}
			}).catch(error => {
					console.log(error)
					this.loading2 = false;
			})
		 },
		 TeacherSuccess(){
			 this.loading2 = true;
			 this.$http.get(utils.UrlConfig.success+"&taskId="+this.taskId+"&tnId="+this.tnId)
				.then(res => {
					res = res.data
					if (res.rtnCode == '0000000') {
							this.getClassroomList2 = [];
							for(var k = 0 ; k < this.getClassroomList.length ; k++){
 							 var json = {};
 							 json.ClassroomList = this.getClassroomList[k].ClassroomList;
							 if(res.bizData.length == 1 && res.bizData[0] == "-1"){
								//  this.getDesc();
								 this.$message.error("调课失败！");
								 this.loading2 = false;
								 return;
							 }
 							 if(k < res.bizData.length){
								 var colorValue = this.getColor(res.bizData[k]);
								 if(colorValue == "cell_white"){
									 json.CurriculumList = {
	 									 "name": this.getClassroomList[k].CurriculumList.name,
	 									 "color": colorValue,
	 									 "roomId": "",
	 									 "index": k,
	 								 };
								 }else{
									 json.CurriculumList = {
	 									 "name": this.getClassroomList[k].CurriculumList.name,
	 									 "color": colorValue,
	 									 "roomId": this.getClassroomList[k].CurriculumList.roomId,
	 									 "index": k,
	 								 };
								 }
 							 }else{
 								 json.CurriculumList = this.getClassroomList[k].CurriculumList;
 							 }
 							 this.getClassroomList2.push(json);
 						 }

							// 	console.log(this.getClassroomList2);
							this.getClassroomList = this.getClassroomList2;
							this.loading2 = false;
					} else {
							this.$message.error(res.msg);
							this.loading2 = false;
					}
			}).catch(error => {
					console.log(error)
					this.loading2 = false;
			})
		},
		getColor(i){
			var colorValue = '';
			switch (i) {
					 case "0":
							 colorValue = "cell_white";
							 //0 白色
							 break;
					 case "1":
							 colorValue = "cell_orange";
							 //1红色
							 break;
					 case "2":
							 colorValue = "cell_yellow";
							 //2黄色
							 break;
					 case "3":
							 colorValue = "cell_green";
							 //3绿色
							 break;
					 default:
							 break;
			}
			return colorValue;
		},
		againBut(){
			sessionStorage.setItem('canAgain', "1");
			this.$router.push({
				path: '../automatic/'
			});
		},
		lookError(){
			this.errorList =[];
			this.loadingError = true;
			this.dialogTableVisible = true;
			this.$http.post(utils.UrlConfig.findRule+"&taskId="+this.taskId+"&tnId="+this.tnId)
				.then(res => {
					res = res.data
					if (res.rtnCode === "0000000") {
						this.loadingError = false;
						this.errorList = res.bizData;
					} else {
							this.$message.error(res.msg);
					}
			}).catch(error => {
					console.log(error)
			})
		},
		getDesc(){
			this.$http.post(utils.UrlConfig.getDesc+"&taskId="+this.taskId+"&tnId="+this.tnId)
				.then(res => {
					res = res.data
					if (res.rtnCode === "0000000") {
							this.$message.error(res.bizData[0]);
					 //  this.$message({
					 // 	 message: res.bizData[0],
					 // 	 type: 'success'
					 //  });
					} else {
							this.$message.error(res.msg);
					}
			}).catch(error => {
					console.log(error)
			})
		},

		}
	}
</script>

<style>
	.textAndBtn {
		margin: 20px 0;
	}

	.selectClass {
		margin-top: 25px;
	}

	.demonstration {
		font-weight: bold;
		font-size: 14px;
		line-height: 36px;
	}

	.noBorderBottom {
		border-bottom: none;
	}

	.export {
		float: right;
		width: 15%;
	}

	.clear {
		clear: both;
	}
	.el-table .cell {
		line-height: 40px;
		padding-left: 0px;
		padding-right: 0px;
	}
	.cell_red{
		background-color: #fff;
		height: 40px;
		color: red;
	}
	.cell_white{
		background-color: #fff;
		height: 40px;
	}
	.cell_green{
		background-color: #33cc99;
		height: 40px;
	}
	.cell_yellow{
		background-color: #ffcc33;
		height: 40px;
	}
	.cell_orange{
		background-color: #ff9966;
		height: 40px;
	}
	.el-table tr:hover{
		background-color: #fff !important;
	}
	#role-scheduling-tab .role-tab ul .active a{
		color: #36c6d3;
	}

	#role-scheduling-tab .role-tab ul li a{
		color: #9B9B9B;
	}

	.againBut{
		width: 122px;
		padding: 8px 0;
		margin-bottom: 35px;
	}

	#role-scheduling-tab .lookError{
		cursor: pointer;
	}

	#role-scheduling-tab .lookError:hover{
				color: #36c6d3;
				text-decoration:underline;
	}

	#role-scheduling-tab .dialogError  .text {
		font-size: 14px;
	}

	#role-scheduling-tab .dialogError .item {
		padding: 14px;
		text-align: left;
	}
	.describe_title{
		float: left;
		font-size: 16px;
		font-weight: bold;
	}
	.describe_title2{
		float: left;
	}
	.describe_colorBox{
		float: left;
		width: 20px;
		height: 20px;
		margin: 3px 10px 0 0;
	}
	.describe_colorBox2{
		float: left;
		width: 20px;
		height: 20px;
		margin: 0px 10px 0 0;
	}
	.green{
		background-color: #33cc99;
	}
	.yellow{
		background-color: #ffcc33;
	}
	.orange{
		background-color: #ff9966;
	}
	.describe_text{
		float: left;
		height: 20px;
		line-height: 20px;
		margin: 3px 40px 0 0;
	}
	.describe_text2{
		float: left;
		height: 20px;
		line-height: 20px;
		margin: 0px 40px 0 0;
	}
	.overflow{
		overflow: hidden;
	}
	.describe_textBox{
		overflow: hidden;
	}
	.describe_item{
		overflow: hidden;
		margin-bottom: 10px;
	}
	.noMB10{
		margin-bottom: 0px;
	}
</style>
